package com.wanglibao.woodtrade.woodtrade;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.xutils.x;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by lugg on 2016/4/5.
 */
public class MyApplication extends Application {

    private static Context context;

    public List<Activity> activityList = new LinkedList<Activity>();

    /**
     * 0没有下载
     * 1正在下载
     */
    public int downCode = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        Fresco.initialize(context);
        getBaseUrl();
        x.Ext.init(this);
    }

    public void getBaseUrl() {
        OkHttpUtils.post()
                .addParams("app_id", "3").addParams("os", "ANDROID").addParams("scenes", "0").addParams("ver_code", "1.0")
                .url("http://setting.wanglibao.com/api.php?app_id=3&os=ANDROID&scenes=0&ver_code=1.0")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(okhttp3.Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(String response) {
                        System.out.println("MyApplication response:" + response);
                        NetBaseUrl baseUrl = new NetBaseUrl();
                        baseUrl = new Gson().fromJson(response, NetBaseUrl.class);
//                        String base_url = baseUrl.getApi_urls().getSERVER();
//                        SharePreferenceUtils.put(getApplicationContext(),"base_url",base_url);
//                        System.out.println("base_url"+SharePreferenceUtils.get(getApplicationContext(),"base_url",""));
                    }
                });
    }

    public static Context getContext() {
        return context;
    }

    public void exit() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancelAll();
        for (Activity activity : activityList) {
            activity.finish();
        }
        System.exit(0);
    }

    public void addActivity(Activity activity) {
        activityList.add(activity);
    }
}
